<?php
require_once(dirname(__FILE__).'/IFeed.class.php');

/**
 * Description of Feed
 *
 * @author agustin
 */
abstract class Feed implements IFeed {

  protected $source;
  protected $domain;
  protected $xml_feed;
  protected $style_sheet;

  public function getStyleSheet(){
    if(!isset($this->style_sheet)){
      $this->setStyleSheet();
    }
    return $this->style_sheet;
  }

  public function setStyleSheet($url_path = null){
    $format = '<link rel="stylesheet" href="%s" type="text/css" media="all"></link>';
    if(is_null($url_path)){
      $url_path = "css/simplefeed.css";
    }
    $this->style_sheet = sprintf($format,$url_path);
  }

  public function setSource($source) {
    $this->source = $source;
    $this->getAll();
  }

  public function setBaseDomain($domain) {
    $this->domain = $domain;
  }

  public function toArray($limit = 0){
    throw new Exception('No implemented HEre');
  }

  public function toList($limit = 0){
    throw new Exception('No implemented HEre');
  }

  private function getAll() {
    if(isset($this->source)) {
      if(get_cfg_var('allow_url_fopen') == 1) {
        if(!($this->xml_feed = simplexml_load_file($this->source))) {
          throw new Exception('Failed Read Source');
        }
      }else{        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->source);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        if(!($this->xml_feed = simplexml_load_string($data))) {
          throw new Exception('Failed Read Source');
        }
      }
    }else {
      throw new Exception('Source not Set');
    }
  }

}
?>
